﻿using FinancialReportPowerPointAddIn.Helper;
using Microsoft.Office.Interop.PowerPoint;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FinancialReportPowerPointAddIn.PPTModule
{
    public class GenerateBalanceSheet
    {
        public static void Write(int year, int month)
        {
            //获取当前ppt中所有的幻灯片
            var slides = Globals.ThisAddIn.Application.ActivePresentation.Slides;
            //在当前选中幻灯片前插入新的幻灯片--代码提示中没有F12进去才找到的方法
            Slide currentslide = slides.Add(slides.Count + 1, PpSlideLayout.ppLayoutBlank);
            currentslide.Select();
            //写入标题           
            Shape titleBox = currentslide.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 40, 15, 470, 15);
            titleBox.TextFrame.TextRange.Text = $"公司资产情况--{year}年{month}月（单位：万元）";
            titleBox.TextFrame.TextRange.Font.Bold = Microsoft.Office.Core.MsoTriState.msoCTrue;
            titleBox.TextFrame.TextRange.Font.Size = 24f;
            //写入表格            
            Shape tableShape = currentslide.Shapes.AddTable(13, 9, 50, 50, currentslide.Master.Width, currentslide.Master.Height - 100);
            Table table = tableShape.Table;
            // 编写标题行
            int currentRow = 1;
            table.Cell(currentRow++, 1).Shape.TextFrame.TextRange.Text = "项目";
            table.Cell(currentRow++, 1).Shape.TextFrame.TextRange.Text = "货币资金";
            table.Cell(currentRow++, 1).Shape.TextFrame.TextRange.Text = "存货";
            table.Cell(currentRow++, 1).Shape.TextFrame.TextRange.Text = "其他流动资产";
            table.Cell(currentRow++, 1).Shape.TextFrame.TextRange.Text = "固定资产";
            table.Cell(currentRow++, 1).Shape.TextFrame.TextRange.Text = "无形资产及长摊费";
            table.Cell(currentRow++, 1).Shape.TextFrame.TextRange.Text = "总资产";
            table.Cell(currentRow++, 1).Shape.TextFrame.TextRange.Text = "银行贷款";
            table.Cell(currentRow++, 1).Shape.TextFrame.TextRange.Text = "其他流动负债";
            table.Cell(currentRow++, 1).Shape.TextFrame.TextRange.Text = "负债合计";
            table.Cell(currentRow++, 1).Shape.TextFrame.TextRange.Text = "股本";
            table.Cell(currentRow++, 1).Shape.TextFrame.TextRange.Text = "利润";
            table.Cell(currentRow++, 1).Shape.TextFrame.TextRange.Text = "负债及所有者权益";
            //开始写入数据
            string[] restaurants = new string[] { "潭江店", "开大店", "白石店", "西粤店", "东湖店", "恩平店", "油十店", "汇总" };
            //写入MTD  要调转颜色RGB =>BGR            
            int currentCol = 2;
            using (var db = new FinancialReportContext())
            {
                foreach (string item in restaurants)
                {
                    currentRow = 1;
                    //写入项目
                    table.Cell(currentRow++, currentCol).Shape.TextFrame.TextRange.Text = item == "汇总" ? "诚福公司" : item;
                    string flag = string.Empty;
                    string code = string.Empty;
                    Common.GetPLNameAndCode(item, out flag, out code);
                    BalanceSheet sheet = db.BalanceSheets.FirstOrDefault(x => x.Restaurant == flag && x.Year == year && x.Month == month);                    
                    if (sheet != null) 
                    {
                        //写入货币资金
                        table.Cell(currentRow++, currentCol).Shape.TextFrame.TextRange.Text = (sheet.货币资金/10000).ToString("#,##0.00");
                        //存货
                        table.Cell(currentRow++, currentCol).Shape.TextFrame.TextRange.Text = (sheet.存货 / 10000).ToString("#,##0");
                        //其他流动资产
                        table.Cell(currentRow++, currentCol).Shape.TextFrame.TextRange.Text = (sheet.其他流动资产小计 / 10000).ToString("#,##0");
                        //固定资产
                        table.Cell(currentRow++, currentCol).Shape.TextFrame.TextRange.Text = (sheet.固定资产账面价值 / 10000).ToString("#,##0");
                        //无形资产及长摊
                        table.Cell(currentRow++, currentCol).Shape.TextFrame.TextRange.Text = (sheet.无形资产及长摊费 / 10000).ToString("#,##0");
                        //总资产
                        table.Cell(currentRow++, currentCol).Shape.TextFrame.TextRange.Text = (sheet.总资产 / 10000).ToString("#,##0");
                        //银行贷款
                        table.Cell(currentRow++, currentCol).Shape.TextFrame.TextRange.Text = (sheet.长期借款 / 10000).ToString("#,##0");
                        //其他流动负债
                        table.Cell(currentRow++, currentCol).Shape.TextFrame.TextRange.Text = (sheet.其他负债小计 / 10000).ToString("#,##0");
                        //负债合计
                        table.Cell(currentRow++, currentCol).Shape.TextFrame.TextRange.Text = (sheet.负债合计 / 10000).ToString("#,##0");
                        //股本
                        table.Cell(currentRow++, currentCol).Shape.TextFrame.TextRange.Text = (sheet.实收资本 / 10000).ToString("#,##0");
                        //利润
                        table.Cell(currentRow++, currentCol).Shape.TextFrame.TextRange.Text = (sheet.未分配利润 / 10000).ToString("#,##0");
                        //负债及所有者权益
                        table.Cell(currentRow++, currentCol).Shape.TextFrame.TextRange.Text = (sheet.负债及所有者权益 / 10000).ToString("#,##0");
                    }
                    currentCol++;
                }
            }
            Common.AutoFit(table);
            Common.OrganizeFormat(table,16,16);          
            // 设置整个表格的单元格边距为无            
            tableShape.Height = currentslide.Master.Height - 100;
            tableShape.Width = currentslide.Master.Width - 100;
            //
            titleBox = currentslide.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, 40, currentslide.Master.Height - 50, 470, 15);
            titleBox.TextFrame.TextRange.Text = $"PS:汇总报表诚福公司抵消了各店之间的内部往来";
            titleBox.TextFrame.TextRange.Font.Bold = Microsoft.Office.Core.MsoTriState.msoCTrue;
            titleBox.TextFrame.TextRange.Font.Size = 18f;
        }
    }
}
